package web.user; /**
 * 文件描述
 *
 * @author 冯根源
 * @date 2020/3/18 21:31
 */

import cn.itcast.commons.CommonUtils;
import domain.user.User;
import service.user.UserException;
import service.user.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件描述
 * 注册Servlet
 *
 * @author 冯根源
 * @date 2020/3/18 21:31
 */
@WebServlet(name = "RegisterServlet", urlPatterns = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");//请求访问编码
        response.setContentType("text/html;charset=utf-8");//响应编码


        //依赖UserService
        UserService userService = new UserService();

        //封装表单数据
        User userForm = CommonUtils.toBean(request.getParameterMap(), User.class);

        //服务器端表单校验
        Map<String, String> errors = new HashMap<>();
        //对用户名进行校验
        String username = userForm.getUsername();
        if (username == null||username.trim().isEmpty()) {
            errors.put("username","用户名不能为空");
        }else if(username.length()<3||username.length()>15){
            errors.put("username","用户长度必须在3~15之间！");
        }
        //对密码进行校验
        String password = userForm.getPassword();
        if (password == null||password.trim().isEmpty()) {
            errors.put("password","密码不能为空");
        }else if(password.length()<3||password.length()>15){
            errors.put("password","密码长度必须在3~15之间！");
        }
        //对表单中验证码格式进行校验
        String sessionVC = (String) request.getSession().getAttribute("session_verifyCode");
        String verifyCode = userForm.getVerifyCode().toUpperCase();
        if (verifyCode == null||verifyCode.trim().isEmpty()) {
            errors.put("verifyCode","验证码不能为空");
        }else if(verifyCode.length()!=4){
            errors.put("verifyCode","验证码长度必须 为4位！");
        }else if (!verifyCode.equals(sessionVC.toUpperCase())){
            errors.put("verifyCode","验证码错误！");
        }

        //判断map是否为空，不为空，说明存在错误
        if(errors !=null&&errors.size()>0){
            /**
             * 保存errors到request
             * 保存form到request域，回显
             * 转发到register.jsp
             */
            request.setAttribute("errors",errors);
            request.setAttribute("user",userForm);
            request.getRequestDispatcher("/user/register.jsp").forward(request, response);
            return;
        }


        //调用userService的register(),传递参数
        try {
            userService.register(userForm);
            response.getWriter().print("<h1>注册成功</h1> <a href ='" + request.getContextPath() + "/login.jsp" + "'>登录</a>");
        } catch (UserException e) {
            request.setAttribute("error", e.getMessage());
            request.setAttribute("user", userForm);
            //转发到register.jsp
            request.getRequestDispatcher("/user/register.jsp").forward(request, response);
        }
    }


}
